package com.hbue.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hbue.springboot.pojo.Result;
import com.hbue.springboot.pojo.TbWorker;
import com.hbue.springboot.service.TbWorkerService;
import com.hbue.springboot.service.WorkerSalaryService;
import com.hbue.springboot.vo.QueryParamsVo;
import com.hbue.springboot.vo.WorkerSalaryVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/account")
@Slf4j
@RequiredArgsConstructor
public class WorkerSalaryController {
    /**
     * 控制器依赖业务逻辑层，此时进行自动装配
     */
    private final WorkerSalaryService workerSalaryService;
    private final TbWorkerService tbWorkerService;
    /**
     * 处理前端的统计工资请求
     * 1 调用业务逻辑对象workerSalaryService的listWorkerSalary方法获取工资信息
     * 2 判断列表是否为空，如果为空：返回“没有找到工资列表” 否则：返回送水工工资列表信息
     * @param query 前端传入的查询参数
     * @return Result
     */
    @PostMapping("/salaryList")
    public Result listWorkerSalary(@RequestBody QueryParamsVo query) {
        log.info("list worker salary ="+query);
        IPage<WorkerSalaryVo> page = workerSalaryService.listWorkerSalary(query);
        return page.getTotal() > 0 ? Result.success(page) : Result.error("没有查询到送水工的工资");
    }

    @PostMapping("/adjustWorkerSalary")
    public Result adjustWorkerSalary(@RequestBody TbWorker worker) {
        log.info("adjustWorkerSalary = "+worker);
        int rows = tbWorkerService.updateWorkerSalaryById(worker);
        return rows > 0 ? Result.success("调整工资成功"): Result.error("调整工资失败");
    }

    @PostMapping("/salary")
    public Result workerSalary(@RequestBody QueryParamsVo query) {
        log.info("workerSalary = "+query.getParams().get("userName"));
        log.info("list worker salary ="+query);
        double pay = workerSalaryService.getSalaryByName((String)query.getParams().get("userName"), query);
        log.info("pay ="+pay);
        if(pay == 0)
        {
            pay = workerSalaryService.getPayByName((String)query.getParams().get("userName"));
        }
        return pay > 0 ? Result.success(pay) : Result.error("没有查询到送水工的工资");
    }

}
